# encoding: utf-8
""" 
@version: v1.0 
@author: autumner 
@license: Apache Licence  
@contact: 18322313385@163.com 
@site:  https://gitee.com/autumner/pythoncookbook
@software: PyCharm 
@file: xmlrpc_client.py 
@time: 2019/7/19 上午10:18
"""
'''
使用xml-rpc进行文件共享
1.编写客户端
'''
from xmlrpc.client import ServerProxy, Fault

from cmd import Cmd
from random import choice
from string import ascii_lowercase
from xmlrpc_server import Node, UNHANDLED
from threading import Thread
from time import sleep
import sys


HEAD_START = 0.1 # 单位为秒
SECRET_LENGTH = 100


def random_string(length):
    """
    返回一个指定长度的由字母组成的随机字符串
    :param length:
    :return:
    """
    chars = []
    letters = ascii_lowercase[:26]
    while length > 0:
        length -= 1
        chars.append(choice(letters))
    return ''.join(chars)

class Client(Cmd):
    """
    一个基于文本的界面，用于访问Node类
    """
    prompt = '>'

    def __init__(self, url, dirname, urlfile):
        """
        设置url、dirname和urlfile， 并在一个独立的线程中启动Node服务器
        :param url:
        :param dirname:
        :param urlfile:
        """
        Cmd.__init__(self)
        self.secret = random_string(SECRET_LENGTH)
        n = Node(url, dirname, self.secret)
        t = Thread(target=n._start)
        t.setDaemon(1)
        t.start()
        # 让服务器先启动起来
        sleep(HEAD_START)
        self.server = ServerProxy(url)
        for line in open(urlfile):
            line = line.strip()
            self.server.hello(line)

    def do_fetch(self, arg):
        # 调用服务器的方法fetch
        try:
            self.server.fetch(arg, self.secret)
        except Fault as f:
            if f.faultCode != UNHANDLED: raise
            print("Couldn't find the file", arg)

    def do_list(self, arg):
        # 获取服务器文件列表
        try:
            self.server.list(self.secret)
        except Fault as f:
            if f.faultCode != UNHANDLED: raise
            print("Couldn't find the file list")

    def do_exit(self, arg):
        # 退出程序
        print()
        sys.exit()


    do_EOF = do_exit # EOF与‘exit’等价


def main():
    urlfile, directory, url = sys.argv[1:]
    client = Client(url, directory, urlfile)
    # client = Client('http://localhost:5454', 'f', 'urls.txt')
    client.cmdloop()


if __name__ == '__main__': main()

"""
EXample usage
Terminal:
echo "http://localhost:4243" > urls.txt
mkdir f #存放下载文件的文件夹
python3 xmlrpc_client.py urls.txt localdir http://localhost:5454
"""